package org.andlisp.android;

import java.io.InputStreamReader;
import java.io.PrintStream;

import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import jp.tyas.libs.lisp.Toplevel;
import jp.tyas.libs.lisp.Symbol;
import jp.tyas.libs.lisp.Builtin;
import jp.tyas.libs.terminal.Terminal;

public class Andlisp extends Activity {
  final static int REQ_RUN_SCRIPT = 1;
  final static int DIALOG_1 = 1;
  final static String LOG_TAG = "Andlisp";
  TerminalView term;
  Terminal.Connection con = null;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (SafePreferenceManager.getBoolean(this, "HideTitleBar", true)) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
    setContentView(R.layout.terminal);

    term = (TerminalView)findViewById(R.id.Terminal);
    con = term.openConnection();

    new Thread () {
      public void run() {
        try {
          PrintStream out = new PrintStream(con.getOutputStream());
          Toplevel lisp = new Toplevel(new InputStreamReader(con.getInputStream()), out);
          lisp.set(new Symbol("*PRIV*", Andlisp.this, null));

          try {
            android.content.res.AssetManager assets = getAssets();
            java.io.Reader r = new InputStreamReader(assets.open("lisp/android/andlisp.l"));
            Builtin.load(r, lisp);
          } catch (java.io.IOException e) {
            e.printStackTrace(out);
          }

          lisp.repl();
        } catch (Exception e) {
          Log.e(LOG_TAG, "couldn't start terminal", e);
        }
      }
    }.start();
  }

  protected void OnStart()
  {
    term.requestFocus();
  }

  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inf = getMenuInflater();
    inf.inflate(R.menu.options_menu, menu);
    return true;
  }

  protected void onActivityResult(int req, int res, Intent data) {
    switch (req) {
    case REQ_RUN_SCRIPT:
      try {
        if ("file".equals(data.getData().getScheme())) {
          Log.i(LOG_TAG, "" + data.getData().getPath());
          String path = data.getData().getPath();
          if (path.endsWith(".l")) {
            LispService.runScriptAsync(this, path, -1, null);
          } else {
            Log.w(LOG_TAG, "script file should ends with .l");
          }
        }
      } catch (Throwable t) {
        Log.e(LOG_TAG, "onActivityResult", t);
      }
      break;
    }
  }

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.run_script:
      try {
        startActivityForResult(new Intent(SafePreferenceManager.getString(this, "PickAction", Intent.ACTION_PICK)), REQ_RUN_SCRIPT);
      } catch (ActivityNotFoundException e) {
        Log.e(LOG_TAG, "run script failure", e);
      }
      return true;
    case R.id.menu_setting:
      startActivity(new Intent(this, SettingActivity.class));
      return true;
    }
    return false;
  }

  protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;

    switch (id) {
    case DIALOG_1:
      dialog = new AlertDialog.Builder(this)
        .setTitle("Hello Dialog")
        .create();
      break;
    }

    return dialog;
  }
}
